﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoboServ
{
    class XBeeMessage
    {
        public byte apiID;
        public byte msgID;
        public byte[] data;

        public bool use16BitAddress = false;
        public UInt16 address16 = 0xFFFF;
        public bool use64BitAddress = false;
        public UInt64 address64 = 0x000000000000FFFF;

        public XBeeMessage()
        {

        }

        public void set16BitAddress(UInt16 addr)
        {
            use16BitAddress = true;
            address16 = addr;
        }

        public void set64BitAddress(UInt64 addr)
        {
            use64BitAddress = true;
            address64 = addr;
        }

        public int getLength()
        {
            int len = (use64BitAddress ? 8 : 0) +
                (use16BitAddress ? 2 : 0) +
                data.Length +
                2; // For apiID, msgID

            return len;
        }

        public int getMessageSum()
        {
            int total = apiID + msgID;

            if (use64BitAddress)
            {
                byte[] addr = Utils.UInt64ToBytes(address64);
                foreach (byte b in addr)
                    total += b;
            }

            if (use16BitAddress)
            {
                byte[] addr = Utils.UInt16ToBytes(address16);
                foreach (byte b in addr)
                    total += b;
            }

            foreach (byte b in data)
                total += b;

            return total;
        }

    }
}
